package com.fruitshop.controller;

import com.fruitshop.common.Result;
import com.fruitshop.dto.CategoryDTO;
import com.fruitshop.dto.ProductDTO;
import com.fruitshop.entity.ProductDetail;
import com.fruitshop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品控制器
 */
@RestController
@RequestMapping("/product")  // 注意：由于已配置context-path为/api，完整路径为/api/product
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 获取所有商品分类
     */
    @GetMapping("/categories")
    public Result<List<CategoryDTO>> getCategories() {
        try {
            List<CategoryDTO> categories = productService.getAllCategories();
            return Result.success("获取分类成功", categories);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "获取分类失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据分类ID获取商品列表
     */
    @GetMapping("/list")
    public Result<List<ProductDTO>> getProductsByCategory(
            @RequestParam(required = false) Integer categoryId) {
        try {
            List<ProductDTO> products = productService.getProductsByCategory(categoryId);
            return Result.success("获取商品列表成功", products);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "获取商品列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据详情ID获取商品详情
     */
    @GetMapping("/detail/{detailId}")
    public Result<ProductDetail> getProductDetail(@PathVariable Integer detailId) {
        try {
            ProductDetail detail = productService.getProductDetail(detailId);
            return Result.success("获取商品详情成功", detail);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "获取商品详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 搜索商品
     */
    @GetMapping("/search")
    public Result<List<ProductDTO>> searchProducts(
            @RequestParam String keyword) {
        try {
            if (keyword == null || keyword.isEmpty()) {
                return Result.fail(400, "请提供搜索关键词");
            }
            
            List<ProductDTO> products = productService.searchProducts(keyword);
            return Result.success("搜索商品成功", products);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "搜索商品失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取热销商品
     */
    @GetMapping("/hot")
    public Result<List<ProductDTO>> getHotProducts(
            @RequestParam(required = false) Integer limit) {
        try {
            List<ProductDTO> products = productService.getHotProducts(limit);
            return Result.success("获取热销商品成功", products);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "获取热销商品失败: " + e.getMessage());
        }
    }
} 